﻿using AnesSystem.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;
using AnesSystem.Services;

namespace AnesSystem.ViewModels
{
    public class SCDCYaoPingHaoCaiCtrlViewModel : BaseViewModel<SCDCYaoPingHaoCaiCtrlViewModel>
    {
        #region Variables
        private AnesSystem.Models.Model.Hospital.SC.SCDC.PatientLinkModel _patientLinkModel;

        private ObservableCollection<ConfigConsumablesModel> _disHaoCaiLeftList = new ObservableCollection<ConfigConsumablesModel>();

        private ObservableCollection<ConfigConsumablesModel> _disHaoCaiRightList = new ObservableCollection<ConfigConsumablesModel>();

        private ConfigConsumablesModel model = null;

        private StringBuilder mzysStr = new StringBuilder();

        private StringBuilder jbysStr = new StringBuilder();

        private int itemCount = 40;

        #endregion


        #region Constructor

        public SCDCYaoPingHaoCaiCtrlViewModel()
        {
            //UpdateDisplayOprList();
        }

        #endregion

        #region Properties

        public PatientLinkModel PatientLinkModel
        {
            get
            {
                return _patientLinkModel;
            }

            set
            {
                _patientLinkModel = value as AnesSystem.Models.Model.Hospital.SC.SCDC.PatientLinkModel;
                BindHaoCaiList();
                BindDoctorText();
            }
        }


        public ObservableCollection<ConfigConsumablesModel> DisHaoCaiLeftList
        {
            get
            {
                return _disHaoCaiLeftList;
            }

            set
            {
                _disHaoCaiLeftList = value;
                RaisePropertyChanged("DisHaoCaiLeftList");
            }
        }

        public ObservableCollection<ConfigConsumablesModel> DisHaoCaiRightList
        {
            get
            {
                return _disHaoCaiRightList;
            }

            set
            {
                _disHaoCaiRightList = value;
                RaisePropertyChanged("DisHaoCaiRightList");
            }
        }

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods
        public void BindHaoCaiList()
        {
            DisHaoCaiLeftList.Clear();
            DisHaoCaiRightList.Clear();
            DisHaoCaiLeftList.Add(new ConfigConsumablesModel()
            {
                Index = "序号",
                Name = "品名",
                Price = "价格",
                Count = "数量"
            });
            DisHaoCaiRightList.Add(new ConfigConsumablesModel()
            {
                Index = "序号",
                Name = "品名",
                Price = "价格",
                Count = "数量"
            });
            if (_patientLinkModel.DeChangTable.ConfigYaoPingHaoCai == null)
            {
                _patientLinkModel.DeChangTable.ConfigYaoPingHaoCai = new SCDCYaoPingHaoCaiModel();
                UpdateDisplayOprList();
            }
            else
            {
                for (int i = 0; i < itemCount; i++)
                {
                    DisHaoCaiLeftList.Add(_patientLinkModel.DeChangTable.ConfigYaoPingHaoCai.HaoCaiLeftList[i]);
                    DisHaoCaiRightList.Add(_patientLinkModel.DeChangTable.ConfigYaoPingHaoCai.HaoCaiRightList[i]);
                    DisHaoCaiLeftList[i].PropertyChanged += Model_PropertyChanged;
                    DisHaoCaiRightList[i].PropertyChanged += Model_PropertyChanged;
                }
            }
        }

        public void UpdateDisplayOprList()
        {
            List<ConfigConsumablesModel> consumablesModelList = DataManager.Instance.ConfigConsumablesModelList.ToList();
            List<ConfigConsumablesModel> leftList = consumablesModelList.Where(O => O.Col == 1).ToList();
            List<ConfigConsumablesModel> rightList = consumablesModelList.Where(O => O.Col == 2).ToList();
            leftList = leftList.OrderBy(O => O.Row).ToList();
            rightList = rightList.OrderBy(O => O.Row).ToList();
            for (int i = 0; i < itemCount; i++)
            {

                if (i >= leftList.Count())
                {
                    model = new ConfigConsumablesModel();
                }
                else
                {
                    model = leftList[i];
                }
                model.Index = i + 1 + "";
                model.PropertyChanged += Model_PropertyChanged;
                DisHaoCaiLeftList.Add(model);
            }
            _patientLinkModel.DeChangTable.ConfigYaoPingHaoCai.HaoCaiLeftList = DisHaoCaiLeftList.ToList();
            _patientLinkModel.DeChangTable.ConfigYaoPingHaoCai.HaoCaiLeftList.RemoveAt(0);
            model = null;
            for (int i = 0; i < itemCount; i++)
            {

                if (i >= rightList.Count())
                {
                    model = new ConfigConsumablesModel();
                }
                else
                {
                    model = rightList[i];
                }
                model.Index = i + 1 + "";
                model.PropertyChanged += Model_PropertyChanged;
                DisHaoCaiRightList.Add(model);
            }
            _patientLinkModel.DeChangTable.ConfigYaoPingHaoCai.HaoCaiRightList = DisHaoCaiRightList.ToList();
            _patientLinkModel.DeChangTable.ConfigYaoPingHaoCai.HaoCaiRightList.RemoveAt(0);
            PatientLinkService.Instance.AddUpdate(_patientLinkModel);
        }

        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            _patientLinkModel.DeChangTable.ConfigYaoPingHaoCai.HaoCaiLeftList = DisHaoCaiLeftList.ToList();
            _patientLinkModel.DeChangTable.ConfigYaoPingHaoCai.HaoCaiRightList = DisHaoCaiRightList.ToList();
            _patientLinkModel.DeChangTable.ConfigYaoPingHaoCai.HaoCaiRightList.RemoveAt(0);
            _patientLinkModel.DeChangTable.ConfigYaoPingHaoCai.HaoCaiLeftList.RemoveAt(0);
            PatientLinkService.Instance.AddUpdate(_patientLinkModel);
        }

        public void BindDoctorText()
        {
            mzysStr.Clear();
            jbysStr.Clear();
            if (PatientLinkModel.AnesthesiaRecord != null)
            {
                AnesthesiaRecordModel aesthesiaRecord = PatientLinkModel.AnesthesiaRecord;
                if (!string.IsNullOrEmpty(aesthesiaRecord.OperDoctor1))
                {
                    AccountModel account = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(aesthesiaRecord.OperDoctor1));
                    if (account != null)
                    {
                        _patientLinkModel.DeChangTable.ConfigYaoPingHaoCai.Zdys = account.TrueName;
                    }
                }
                if (!string.IsNullOrEmpty(aesthesiaRecord.Zmys))
                {
                    AccountModel account = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(aesthesiaRecord.Zmys));
                    if (account != null)
                    {
                        mzysStr.Append(account.TrueName + "    ");
                    }
                }
                if (PatientLinkModel.Status.InRoomTime != new DateTime())
                {
                    mzysStr.Append(PatientLinkModel.Status.InRoomTime.ToString(DateFormat.DATE_FORMAT_STR_HM) + "----");
                }
                if (aesthesiaRecord.Jbys1StartTime != new DateTime())
                {
                    mzysStr.Append(aesthesiaRecord.Jbys1StartTime.ToString(DateFormat.DATE_FORMAT_STR_HM));
                }
                else if (PatientLinkModel.Status.OutRoomTime != new DateTime())
                {
                    mzysStr.Append(PatientLinkModel.Status.OutRoomTime.ToString(DateFormat.DATE_FORMAT_STR_HM));
                }

                if (!string.IsNullOrEmpty(aesthesiaRecord.Jbys2))
                {
                    AccountModel account = DataManager.Instance.AccountList.ToList().Find(O => O.Id.ToString().Equals(aesthesiaRecord.Jbys2));
                    if (account != null)
                    {
                        jbysStr.Append(account.TrueName + "    ");
                    }
                    if (aesthesiaRecord.Jbys1StartTime != new DateTime())
                    {
                        jbysStr.Append(aesthesiaRecord.Jbys1StartTime.ToString(DateFormat.DATE_FORMAT_STR_HM) + "----");
                    }
                    if (PatientLinkModel.Status.OutRoomTime != new DateTime())
                    {
                        jbysStr.Append(PatientLinkModel.Status.OutRoomTime.ToString(DateFormat.DATE_FORMAT_STR_HM));
                    }
                }
                _patientLinkModel.DeChangTable.ConfigYaoPingHaoCai.Mzys = mzysStr.ToString();
                _patientLinkModel.DeChangTable.ConfigYaoPingHaoCai.Jbys = jbysStr.ToString();
            }
        }

        #endregion

        #region EventHandlers

        #endregion
    }
}
